package com.capgemini.fs.app.ekms.service.implementations;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

import com.capgemini.fs.app.ekms.model.Forum;
import com.capgemini.fs.app.ekms.model.Reply;
import com.capgemini.fs.app.ekms.model.Topic;
import com.capgemini.fs.app.ekms.model.User;
import com.capgemini.fs.app.ekms.persistence.PersistenceService;
import com.capgemini.fs.app.ekms.persistence.dao.TopicDAO;
import com.capgemini.fs.app.ekms.service.TopicService;

public class TopicServiceImpl implements TopicService {
	PersistenceService persistenceService = null;

	public PersistenceService getPersistenceService() {
		return persistenceService;
	}

	public void setPersistenceService(PersistenceService persistenceService) {
		this.persistenceService = persistenceService;
	}

	public void Create(Topic topic) {
		getTopicDAO().save(topic);
	}

	public void updateTopic(Topic topic) {
		getTopicDAO().evict(topic);
		Topic exist = getTopicDAO()
				.findByPrimaryKey(Topic.class, topic.getId());
		exist.setSubject(topic.getSubject());
		exist.setContent(topic.getContent());
		exist.setAuthor(topic.getAuthor());
		exist.setStatus(topic.getStatus());
		exist.setCreateDate(topic.getCreateDate());
		exist.setCreator(topic.getCreator());
		exist.setLastModifier(topic.getLastModifier());
		exist.setLastUpdateDate(topic.getLastUpdateDate());
		getTopicDAO().update(exist);

	}

	public TopicDAO getTopicDAO() {
		return getPersistenceService().getTopicDAO();
	}

	public Topic getTopicById(Integer id) {
		return getTopicDAO().get(Topic.class, id);
	}

	public void createTopic(Topic topic, User author, Forum forum) {
		forum = getPersistenceService().getForumDAO().get(Forum.class,
				forum.getId());
		author = getPersistenceService().getUserDAO().get(User.class,
				author.getId());
		topic.setAuthor(author);
		topic.setForum(forum);
		author.getTopics().add(topic);
		forum.getTopics().add(topic);
		Create(topic);
		getPersistenceService().getUserDAO().update(author);
		getPersistenceService().getForumDAO().update(forum);
	}

	// Get Replies Counts to current topic
	public Integer getRepliesCount(Topic topic) {
		topic = getTopicById(topic.getId());
		return topic.getReplies().size();
	}

	// Get The Last Reply To this Topic
	public Reply getLastPost(Topic topic) {
		topic = getTopicDAO().get(Topic.class, topic.getId());
		Set<Reply> allReplies = topic.getReplies();
		if (0 == allReplies.size()) {
			return null;
		} else {
			List<Reply> replyList = new ArrayList<Reply>();
			for (Reply reply : allReplies) {
				replyList.add(reply);
			}
			Collections.sort(replyList);
			Collections.reverse(replyList);
			return replyList.get(0);
		}
	}

	// Comparator for sorting Reply list
	Comparator<Reply> compareByReplyCreateDate = new Comparator<Reply>() {
		public int compare(Reply r1, Reply r2) {
			return r1.getCreateDate().compareTo(r2.getCreateDate());
		}
	};

	// Query topic using subject and status
	public List<Topic> queryTopic(Integer forumId,String subject, Integer status) {
		return getTopicDAO().queryTopics(forumId,subject, status);
	}

}
